#!/usr/bin/php
<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 * */

//$FROM = $argv[1];

function toarray($file,$encode = true) {
	$in = file($file);
	$result = array();
	$isfuzzy=false;
	foreach($in as $line) {
		//echo "Line: ".$line."\n";
		
		if (strpos($line,"#, fuzzy")===0) {
			$isfuzzy=true;
		}
		
		if (isset($msgstr)) {
			if (strpos($line,"\"")===0) {
				//echo "pos:".strpos($line,"\"");
				$s = strpos($line,"\"")+1;
				$e = strrpos($line,"\"")-$s;
				if ($encode)
					$msgstr.=tohtml(substr($line,$s,$e));
				else 
					$msgstr.=substr($line,$s,$e);
			} else {					
				if ($msgstr=="") $msgstr=$msgid;
				if ($isfuzzy) $msgstr=$msgid; 
				$result[$msgctxt]=$msgstr;
				//echo "save: ".$msgstr."\n";
				unset($msgctxt);
				unset($msgid);
				unset($msgstr);
				$isfuzzy=false;
			}
		} elseif (isset($msgid)) {
			if (strpos($line,"\"")===0) {
				//echo "pos:".strpos($line,"\"");
				$s = strpos($line,"\"")+1;
				$e = strrpos($line,"\"")-$s;
				if ($encode)
					$msgid.=tohtml(substr($line,$s,$e));
				else 
					$msgid.=substr($line,$s,$e);
			}
		}
		
		if (strpos($line,"msgctxt")===0 and strpos($line,"msgctxt")!==false) {
			$s = strpos($line,"\"")+1;
			$e = strrpos($line,"\"")-$s;
			$msgctxt=substr($line,$s,$e);
		} else {
			if (isset($msgctxt)) {
				if (strpos($line,"msgid")===0 and strpos($line,"msgid")!==false) {
					$s = strpos($line,"\"")+1;
					$e = strrpos($line,"\"")-$s;
					if ($encode)
						$msgid=tohtml(substr($line,$s,$e));
					else 
						$msgid=substr($line,$s,$e);
				} elseif (strpos($line,"msgstr")===0 and strpos($line,"msgstr")!==false) {
					$s = strpos($line,"\"")+1;
					$e = strrpos($line,"\"")-$s;
					//$msgstr=str_replace("&amp;","&",htmlentities(substr($line,$s,$e),ENT_QUOTES,"UTF-8"));
					if ($encode)
						$msgstr=tohtml(substr($line,$s,$e));
					else 
						$msgstr=substr($line,$s,$e);
				}
				
			}
		}
		/*
		echo "ctxt: ".$msgctxt."\n";
		echo "id: ".$msgid."\n";
		echo "str: ".$msgstr."\n";
		*/
	}
	// remanente
	if (isset($msgstr)) {
		if ($msgstr=="") $msgstr=$msgid;
		if ($isfuzzy) $msgstr=$msgid; 
		$result[$msgctxt]=$msgstr;
		//echo "save: ".$msgstr."\n";
		unset($msgctxt);
		unset($msgid);
		unset($msgstr);
	}
		
	return $result;
}

function tohtml($string) {
	$entities = array("á" => "&aacute;","é" => "&eacute;","í" => "&iacute;","ó" => "&oacute;","ú" => "&uacute;","ñ" => "&ntilde;","Ñ" => "&Ntilde;","\$" => "\\$");
	foreach($entities as $key => $value) {
		$string = str_replace($key,$value,$string);
	}
	return $string;
}

function writearray($array,$using,$file) {
	$out = fopen($file,"w");

	fwrite($out,"<?php\r\n");
	fwrite($out,"global ".$using.";\r\n");	
	foreach($array as $key => $value) {
		$string = $using."['".$key."']=\"".$value."\";\r\n";
		fwrite($out,$string);
	}
	fwrite($out,"?>\r\n");
	fclose($out);
}

$fpos = strrpos(dirname(__FILE__), "/");
if ($fpos<=0) {
	$fpos = strrpos(dirname(__FILE__), "\\");
}

define('ROOT_PATH',substr(dirname(__FILE__), 0, $fpos)."/");

$dir=opendir(".");
$not = array(".","..","pot",".svn");

while ($item = readdir($dir)) {
	if (is_dir($item) and in_array($item,$not)===false) {
		echo "Making ".$item."\n";
	
		$lang = toarray($item."/poat_lang.".$item.".po");
		writearray($lang,"$"."LANG",ROOT_PATH."lang/lang_".strtolower($item).".inc");

		$lang = toarray($item."/poat_mail_lang.".$item.".po",false);
		writearray($lang,"$"."MAIL",ROOT_PATH."lang/mail_lang_".strtolower($item).".inc");
	}
}

echo "Done makeinc";
?>
